package com.gxust.mapper;

import com.gxust.pojo.Cart;
import com.gxust.pojo.Product;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface CartMapper {
    @Select("SELECT cart.CartID,product.ProductID,product.ProductImageURL,product.ProductName,product.ProductPrice,cart.Quantity AS TotalQuantity,cart.Quantity * product.ProductPrice AS TotalPrice FROM cart JOIN product ON cart.ProductID = product.ProductID WHERE cart.UserID = #{id} AND product.ProductID = #{productId};\n")
    Map<String, Object> getCartById(Integer id, Integer productId);

    @Insert("insert into cart(productId,userId,Quantity) " +
            "values (#{productId}, #{userId}, #{quantity})")
    void insert(Cart cart);


    @Select("SELECT cart.CartID,product.ProductID,product.ProductImageURL,product.ProductName,product.ProductPrice,cart.Quantity AS TotalQuantity,SUM(cart.Quantity * product.ProductPrice) AS TotalPrice FROM cart JOIN product ON cart.ProductID = product.ProductID WHERE cart.UserID = #{id}\n" +
            "GROUP BY product.ProductID,product.ProductImageURL,product.ProductName,product.ProductPrice,cart.CartID")
    List<Map<String, Object>> list(Integer id);

    @Delete("delete from cart where CartID = #{id}")
    void del(Integer id);

    @Update("update cart set Quantity = #{num} where CartID = #{id}")
    void update(Integer id,Integer num);

    @Select("select * from cart where UserID = #{userId} and ProductID = #{productId}")
    Cart cartList(Integer userId, Integer productId);

    @Delete("delete from cart where userID = #{userId} and ProductID = #{id}")
    void deleteByUserProductId(Integer userId, Integer id);
}
